iOS UIView添加阴影效果 四周或者单边阴影

一般情况下设置阴影只需要设置layer就行,但是如果只是单边阴影,只设置layer效果不是多好,可以用贝塞尔曲线实现。

1. 直接设置layer阴影效果
1
2
3
4
5
6
7
8
9
10
11
12
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 400, 50, 50)];
imageView.layer.shadowColor = [UIColor redColor].CGColor;
//剪切边界 如果视图上的子视图layer超出视图layer部分就截取掉 如果添加阴影这个属性必须是NO 不然会把阴影切掉
imageView.layer.masksToBounds = NO;
//阴影半径,默认3
imageView.layer.shadowRadius = 3;
//shadowOffset阴影偏移,默认(0, -3),这个跟shadowRadius配合使用
imageView.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
// 阴影透明度,默认0
imageView.layer.shadowOpacity = 0.9f;
imageView.backgroundColor = [UIColor greenColor];
[self.view addSubview:imageView];

阴影效果

2. 利用贝塞尔曲线实现

主要代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- (void)viewShadowPathWithColor:(UIColor *)shadowColor shadowOpacity:(CGFloat)shadowOpacity shadowRadius:(CGFloat)shadowRadius shadowPathType:(LeShadowPathType)shadowPathType shadowPathWidth:(CGFloat)shadowPathWidth{

self.layer.masksToBounds = NO;//必须要等于NO否则会把阴影切割隐藏掉
self.layer.shadowColor = shadowColor.CGColor;// 阴影颜色
self.layer.shadowOpacity = shadowOpacity;// 阴影透明度,默认0
self.layer.shadowOffset = CGSizeZero;//shadowOffset阴影偏移,默认(0, -3),这个跟shadowRadius配合使用
self.layer.shadowRadius = shadowRadius;//阴影半径,默认3
CGRect shadowRect = CGRectZero;
CGFloat originX,originY,sizeWith,sizeHeight;
originX = 0;
originY = 0;
sizeWith = self.bounds.size.width;
sizeHeight = self.bounds.size.height;

if (shadowPathType == LeShadowPathTop) {
shadowRect = CGRectMake(originX, originY-shadowPathWidth/2, sizeWith, shadowPathWidth);
}else if (shadowPathType == LeShadowPathBottom){
shadowRect = CGRectMake(originY, sizeHeight-shadowPathWidth/2, sizeWith, shadowPathWidth);
}else if (shadowPathType == LeShadowPathLeft){
shadowRect = CGRectMake(originX-shadowPathWidth/2, originY, shadowPathWidth, sizeHeight);
}else if (shadowPathType == LeShadowPathRight){
shadowRect = CGRectMake(sizeWith-shadowPathWidth/2, originY, shadowPathWidth, sizeHeight);
}else if (shadowPathType == LeShadowPathCommon){
shadowRect = CGRectMake(originX-shadowPathWidth/2, 2, sizeWith+shadowPathWidth, sizeHeight+shadowPathWidth/2);
}else if (shadowPathType == LeShadowPathAround){
shadowRect = CGRectMake(originX-shadowPathWidth/2, originY-shadowPathWidth/2, sizeWith+shadowPathWidth, sizeHeight+shadowPathWidth);
}
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:shadowRect];
self.layer.shadowPath = bezierPath.CGPath;//阴影路径
}

阴影效果.gif

移步到GitHub项目地址
)

-------------本文结束感谢您的阅读-------------

本文标题:iOS UIView添加阴影效果 四周或者单边阴影

文章作者:leon

发布时间:2019年04月22日 - 00:04

最后更新:2019年04月25日 - 16:04

原始链接:https://huanghaipo.github.io/2019/04/22/iOS UIView添加阴影效果 四周或者单边阴影/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

坚持原创技术分享,您的支持将鼓励我继续创作!